package edu.sctu.zcy.month201506.day20150616;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user24 on 2015/6/16.
 */
public class URLtest {

    URL url;
    public URLtest(String path) {
        try {
            this.url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public void url(){

        int count = 0;
        byte[] buffer = new byte[1024];
        try {
            InputStream inputStream = url.openStream();

            while ((count = inputStream.read(buffer)) != -1){
                System.out.println(new String(buffer,0,count,"utf-8"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void urlConnecttion(){

        int count = 0;
        byte[] buffer = new byte[1024];
        try {

            URLConnection urlConnection = url.openConnection();




            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();

            while ((count = inputStream.read(buffer)) != -1){
                System.out.println(new String(buffer,0,count,"utf-8"));
            }
            System.out.println(urlConnection.getContent());
            System.out.println(urlConnection.getContentEncoding());
            System.out.println(urlConnection.getContentLength());
            System.out.println(urlConnection.getContentType());

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
